<?php require_once('connections/con_gl.php'); require_once("excel/excelwriter.class.php");?>

<?php

cekAkses($_SESSION[akses],"L-3");

echo $_SESSION[nama];

mysql_select_db($database_con_gl, $con_gl);

mysql_query("delete from gl_trans where total='0'", $con_gl);

$currentPage = $_SERVER["PHP_SELF"];



$maxRows_data = 20;

$pageNum_data = 0;

if (isset($_GET['pageNum_data'])) {

  $pageNum_data = $_GET['pageNum_data'];

}

$startRow_data = $pageNum_data * $maxRows_data;

$cr_txt = str_replace("+"," ",$_REQUEST[cr_txt]);

mysql_select_db($database_con_gl, $con_gl);

$query_data = "SELECT * from v_rekapumurpiutangretail a";
$query_korporate = "SELECT * from v_rekapumurpiutangkorporate a";

/*
if(!strstr($_SESSION[akses],'A-1-4')) {

	$query_data .= " AND gudang='$_SESSION[gudang]'";

}
*/
if($_GET[gudang]<>'ALL' AND $_GET[gudang]<>'') { 

$query_data .=" where a.gudang='$_GET[gudang]'";
$query_korporate  .=" where a.gudang='$_GET[gudang]'";

}
else{
$query_data .=" where a.gudang='$_SESSION[outlet]'";
$query_korporate  .=" where a.gudang='$_SESSION[outlet]'";
}



if($_GET[mulai]<>'' AND $_GET[sampai]<>'') { 

$query_data .=" AND a.checkin between '$_GET[mulai]' AND '$_GET[sampai]'";
$query_korporate .=" AND a.mulai between '$_GET[mulai]' AND '$_GET[sampai]'";
}


//print_r($query_data);
if(!isset($_GET['mulai']) && !isset($_GET['sampai']))
{
 
}
else
{
$mulai =$_GET['mulai'];
$akhir =$_GET['sampai'];
$query_data .=" order by a.checkin desc";
$query_korporate .=" order by a.mulai desc";


$cekdata = mysql_query($query_data, $con_gl) or die(mysql_error());
$row_data = mysql_fetch_assoc($cekdata);

$cekdatakorporate = mysql_query($query_korporate, $con_gl) or die(mysql_error());
$row_datakorporate = mysql_fetch_assoc($cekdatakorporate);
}



include('include/widget_export_rekappiutang.php');

?>

<link href="css/ui.datepicker.css" type="text/css" rel="stylesheet" />

<script language="javascript">

function download()

{

	window.location='report_penjualan.xls';

}

</script>

<script type="text/javascript" src="js/ui.datepicker.js"></script>

<script type="text/javascript">

<!--

$(function()

      {

        $('.calendar').datepicker({

            appendText : "",

            dateFormat : 'yy/mm/dd'

          });

      });

</script>

<script type="text/javascript">

$(document).ready(function(){

$(".flipsearch").click(function(){

	$("form.tanggalsearch").hide() 

    $(".objsearch").slideToggle("fast");

  });

});



$(document).ready(function(){

$(".fliptanggal").click(function(){

	$("form.objsearch").hide();

    $(".tanggalsearch").slideToggle("fast");

  });

});

</script>

<h2>REKAPITULASI UMUR PIUTANG DAGANG</h2>
<table width="100%" border="0" align="center" cellpadding="5" cellspacing="0" class="datatable"> 

  <tr valign="top" bgcolor="#FFFFFF" class="hide">

    <td> Pencarian</td>

    <td colspan="7" align="left"><?php

	mysql_select_db($database_con_gl, $con_gl);

$query_cr_gud = "SELECT id, gudang FROM gl_gudang";

if(!strstr($_SESSION[akses],'A-1-4')) {

	$query_cr_gud .= " where id='$_SESSION[lokasi]' ";

}

$query_cr_gud .= " ORDER BY gudang ASC";

$cr_gud = mysql_query($query_cr_gud, $con_gl) or die(mysql_error());

$row_cr_gud = mysql_fetch_assoc($cr_gud);

$totalRows_cr_gud = mysql_num_rows($cr_gud);

?>

	<form id="search" name="search" method="post" action="">

<label><select name="cr_gudang">

	      <option value="ALL">Pilih Cabang</option>

	      <?php

do {  

?>

	      <option value="<?php echo $row_cr_gud['gudang']?>" <?php if($_GET['gudang']==$row_cr_gud['gudang']) echo "selected"; ?>><?php echo $row_cr_gud['gudang']?></option>

	      <?php

} while ($row_cr_gud = mysql_fetch_assoc($cr_gud));

  $rows = mysql_num_rows($cr_gud);

  if($rows > 0) {

      mysql_data_seek($cr_gud, 0);

	  $row_cr_gud = mysql_fetch_assoc($cr_gud);

  }

?>

        </select></label>

	    <label>

      Tanggal : 
      <input name="mulai" type="text" id="mulai" value="<?php echo $_GET[mulai];?>" size="15" maxlength="15" class="calendar"/>
	    </label> 
	    s/d <label>
	    <input name="sampai" type="text" id="sampai" value="<?php echo $_GET[sampai];?>" size="15" maxlength="15" class="calendar"/>
	    </label>
	    <label>
          <input name="Search" type="submit" id="Search" value="Search" />
        </label>
	  </form>

	<?php

	if(isset($_POST['Search'])) {

		echo "<script>window.location=\"index.php?component=rekappiutang&mulai=".$_POST[mulai]."&sampai=".$_POST[sampai]."&cr_txt=".str_replace(" ","+",$_POST[cr_txt])."&gudang=".$_POST[cr_gudang]."\";</script>";

	}

	?></td>
  </tr>   
   <tr bgcolor="#CCCCCC">
      <th width="200">Customer</th>
   <th width="78"><strong>0 -30 hari</strong></th>
    <th width="78"><strong>31 - 60 hari</strong></th>
	<th width="78">61 - 90</th>
	<th width="78">91 - 120 hari</th>
	<th width="100">Lebih dari 120 hari</th>
	<th width="100" align="center"><strong>Total</strong></th>
    </tr>
	
	      <tr valign="top">
          <td colspan="7" align="left"><h2><strong>PIUTANG SEWA RETAIL</strong></h2></td>
        </tr>
        <?php do { ?>
        <tr valign="top">
          <td align="left"><strong><?php echo $row_data['nama'];?></strong></br><?php echo $row_data['alamat'];?></td>
          <td align="center"><?php echo number_format($row_data['satu'],0,',','.').",-";?></td>
          <td align="center"><?php echo number_format($row_data['dua'],0,',','.').",-"; ?></td>
          <td align="center"><?php echo number_format($row_data['tiga'],0,',','.').",-"; ?></td>
          <td align="center"><?php echo number_format($row_data['empat'],0,',','.').",-"; ?></td>
          <td align="center"><?php echo number_format($row_data['lima'],0,',','.').",-"; ?></td>
          <td align="right"><?php echo number_format($row_data['satu'] + $row_data['dua'] + $row_data['tiga'] + $row_data['empat']+$row_data['lima'],0,',','.').",-"; ?></td>
        </tr>
			<?php 
        $totalsatu += $row_data['satu'];
        $totaldua += $row_data['dua'];
        $totaltiga += $row_data['tiga'];
		$totalempat += $row_data['empat'];
        $totallima += $row_data['totalbayar'];
        $totalretail += $row_data['satu'] + $row_data['dua'] + $row_data['tiga'] + $row_data['empat']+$row_data['lima'];
        } while ($row_data = mysql_fetch_assoc($cekdata)); ?>
        <tr valign="top">
          <td align="center"><span style="font-weight: bold">SUB TOTAL :</span></td>
          <td align="center"><strong><?php echo number_format($totalsatu,0,',','.').",-";?></strong></td>
          <td align="center"><strong><?php echo number_format($totaldua,0,',','.').",-";?></strong></td>
          <td align="center"><strong><?php echo number_format($totaltiga,0,',','.').",-";?></strong></td>
          <td align="center"><strong><?php echo number_format($totalempat,0,',','.').",-";?></strong></td>
          <td align="center"><strong><?php echo number_format($totallima,0,',','.').",-";?></strong></td>
          <td align="right"><strong><?php echo number_format($totalretail,0,',','.').",-";?></strong></td>
        </tr>
        
        <tr valign="top">
          <td colspan="7" align="left"><h2><strong>PIUTANG SEWA KORPORATE</strong></h2></td>
        </tr>
        <?php do { ?>
    <tr valign="top">
      <td align="left"><strong><?php echo $row_datakorporate['nama'];?></strong></br>
      <?php echo $row_datakorporate['alamat'];?></td>
	  <td height="47" align="center"><?php echo number_format($row_datakorporate['satu'],0,',','.').",-";?></td>
      <td align="center"></a><?php echo number_format($row_datakorporate['dua'],0,',','.').",-"; ?></td>
      <td align="center"><?php echo number_format($row_datakorporate['tiga'],0,',','.').",-"; ?></td>
      <td align="center"><?php echo number_format($row_datakorporate['empat'],0,',','.').",-"; ?></td>
      <td align="center"><?php echo number_format($row_datakorporate['lima'],0,',','.').",-"; ?></td>
      <td align="right"><?php echo number_format($row_datakorporate['satu'] + $row_datakorporate['dua'] + $row_datakorporate['tiga'] + $row_datakorporate['empat']+$row_datakorporate['lima'],0,',','.').",-"; ?></td>
    </tr>
	<?php 
        $totalsatukorporate += $row_datakorporate['satu'];
        $totalduakorporate += $row_datakorporate['dua'];
        $totaltigakorporate += $row_datakorporate['tiga'];
		$totalempatkorporate += $row_datakorporate['empat'];
        $totallimakorporate += $row_datakorporate['totalbayar'];
        $totalkorporate += $row_datakorporate['satu'] + $row_datakorporate['dua'] + $row_datakorporate['tiga'] + $row_datakorporate['empat']+$row_datakorporate['lima'];
        } while ($row_datakorporate = mysql_fetch_assoc($cekdatakorporate)); ?>
    <tr valign="top">
      <td align="center"><strong>SUBTOTAL</strong></td>
      <td align="center"><strong><?php echo number_format($totalsatukorporate,0,',','.').",-";?></strong></td>
      <td align="center"><strong><?php echo number_format($totalduakorporate,0,',','.').",-";?></strong></td>
      <td align="center"><strong><?php echo number_format($totaltigakorporate,0,',','.').",-";?></strong></td>
      <td align="center"><strong><?php echo number_format($totalempatkorporate,0,',','.').",-";?></strong></td>
      <td align="center"><strong><?php echo number_format($totallimakorporate,0,',','.').",-";?></strong></td>
      <td align="right"><strong><?php echo number_format($totalkorporate,0,',','.').",-";?></strong></td>
  </tr>
    <tr valign="top">
      <td colspan="6" align="right">&nbsp;</td>
      <td align="right">&nbsp;</td>
</tr>
    <tr valign="top">
      <td align="center"><strong>GRAND TOTAL</strong></td>
      <td align="center">&nbsp;</td>
      <td align="center">&nbsp;</td>
      <td align="center">&nbsp;</td>
      <td align="center">&nbsp;</td>
      <td align="center">&nbsp;</td>
      <td align="right"><strong><?php echo number_format($totalretail+$totalkorporate,0,',','.').",-";?></strong></td>
  </tr>

  <tr class="hide">

    <td colspan="6"><?php if(strstr($_SESSION['akses'],"L-3")) { ?><?php } ?></td>
  </tr>
</table>

<?php

mysql_free_result($cr_gud);

?>

